package com.lwj.liwanjia.adapter.fabu;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.fabu.FzBean;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.util.List;

/**
 * Created by Administrator on 2018/1/13.
 */

public class FabuFzAdapter extends SuperBaseAdapter<FzBean> {

    private List<FzBean> list;
    private onTextChangeListener mTextListener;
    private boolean flag;

    //设置自定义接口成员变量
    public void setOnTextChangeListener(onTextChangeListener onTextChangeListener) {
        this.mTextListener = onTextChangeListener;
    }

    public FabuFzAdapter(Context context, List<FzBean> data) {
        super(context, data);
        list = data;
        flag = true;
    }

    @Override
    protected void convert(BaseViewHolder holder, FzBean item, final int position) {
        holder.setOnClickListener(R.id.iv_delete_item, new OnItemChildClickListener());
        holder.setVisible(R.id.iv_delete_item, flag);
        holder.setText(R.id.edit_yuanjia, item.getCommodityprice())
                .setText(R.id.edit_zhekoujia, item.getCommodityvipprice())
                .setText(R.id.edit_kucunliang, item.getStock())
                .setText(R.id.edit_yanse, item.getColour())
                .setText(R.id.edit_chicun, item.getClosesize());
        EditText yuanJia = holder.getView(R.id.edit_yuanjia);
        EditText zheKoujia = holder.getView(R.id.edit_zhekoujia);
        EditText kuCunliang = holder.getView(R.id.edit_kucunliang);
        EditText yanSe = holder.getView(R.id.edit_yanse);
        EditText chiCun = holder.getView(R.id.edit_chicun);
        OnTextListener("宝贝价格", yuanJia, position);
        OnTextListener("折扣价", zheKoujia, position);
        OnTextListener("库存量", kuCunliang, position);
        OnTextListener("颜色", yanSe, position);
        OnTextListener("尺寸", chiCun, position);
        setEnable(yuanJia, flag);
        setEnable(zheKoujia, flag);
        setEnable(kuCunliang, flag);
        setEnable(yanSe, flag);
        setEnable(chiCun, flag);
    }

    @Override
    protected int getItemViewLayoutId(int position, FzBean item) {
        return R.layout.fz_item;
    }

    //回传值给Activity
    public interface onTextChangeListener {
        void onTextChanged(String type, int pos, String str);
    }

    public void OnTextListener(final String type, final EditText et, final int position) {
        final TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et.hasFocus()) {//判断当前EditText是否有焦点在
                    mTextListener.onTextChanged(type, position, s.toString());
                }
            }
        };
        //设置EditText的焦点监听器判断焦点变化，当有焦点时addTextChangedListener，失去焦点时removeTextChangedListener
        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    et.addTextChangedListener(textWatcher);
                } else {
                    et.removeTextChangedListener(textWatcher);
                }
            }
        });
    }

    //添加数据
    public void addData(int position) {
        FzBean fzBean = new FzBean();
        list.add(position, fzBean);
        notifyItemInserted(position);
        notifyDataSetChanged();
    }

    // 删除数据
    public void removeData(int position) {
        list.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    //设置是否可以点击
    public void setEditEnable(boolean flag) {
        this.flag = flag;
        notifyDataSetChanged();
    }

    private void setEnable(EditText editText, boolean flag) {
        editText.setEnabled(flag);
    }
}
